﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Time.Model;

namespace Time.App
{
    public partial class AddEditLicenseWindow : Form
    {
        private TimeDataContext db = new TimeDataContext();
        public AddEditLicenseWindow(Form owner)
        {
            this.Owner = owner;
            InitializeComponent();
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            btnSave.Enabled =
                (txtLicenseName.Text.Length > 0
                && txtOrganizationNumber.Text.Length == 11);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                License license = new License { Name = txtLicenseName.Text, OrganizationNumber = txtOrganizationNumber.Text.Replace("-", ""), PublicId = Guid.NewGuid() };
                db.Licenses.InsertOnSubmit(license);

                db.SubmitChanges();
                
                MessageBox.Show(this, "License " + license.Name + " created!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
